﻿Imports System.Text
Imports System.DataExecutorServices
Imports System.Data.SqlClient
Imports Microsoft.SqlServer.Management.Smo
Imports Microsoft.SqlServer.Management.Common
Imports Demo.Data.Objects
Imports System.Reflection

Module Program

    Private ReadOnly DemoDatabaseName As String = Data.DataCallAttribute.DemoDatabase
    Private ReadOnly conn As String = Data.DataCallAttribute.DemoConnectionString
    Private ReadOnly Server As New Server(New ServerConnection(New SqlConnection(conn)))
    Private ReadOnly ThisAssembly As Assembly = GetType(Program).Assembly
    Private DemoDB As Database

    Sub Main()
        If SetupDemoDatabase() Then
            System.ExecutorServices.AddDefaultTrace()
            'note that the project type needs to be changed to console for DoConsoleDemo() to work
            'DoConsoleDemo()
            DoUIDemo()
        End If
    End Sub

    Private Sub DoUIDemo()
        ExecuteProcs()
        Dim frm As New AspectsHarness.Forms.Main With {.Assy = ThisAssembly}
        frm.ShowDialog()
    End Sub

    Private Sub DoConsoleDemo()
        ConsoleDemo.DoConsoleDemo(ThisAssembly)
    End Sub

    Private Function SetupDemoDatabase() As Boolean
        Dim ret As Boolean = False
        Try
            SetupDemoDB()
            UpdateDemoSprocContracts()
            ret = True
        Catch ex As ConnectionException
            Dim msg As String = "Connection failed." & Environment.NewLine & conn & Environment.NewLine & AspectsHarness.Utils.GetFullExceptionMessage(ex)
            Trace.WriteLine(msg, "EXCEPTION")
        Catch ex As Exception
            Trace.WriteLine(AspectsHarness.utils.GetFullExceptionMessage(ex), "EXCEPTION")
        End Try
        Return ret
    End Function

    Private Sub SetupDemoDB()
        Console.WriteLine("setting up AspectDemo DB...")
        'NOTE: this appears to not discover a running instance of SQL Server 2005
        'If Server.Name = My.Computer.Name Then
        '    Dim dt As DataTable = System.Data.Sql.SqlDataSourceEnumerator.Instance.GetDataSources
        '    If dt.Rows.Count = 0 Then Throw New ConnectionException("No instances of SQL Server appear to be running on this machine. This demo requires SQL Server.")
        'End If
        If Server.Databases.Contains(DemoDatabaseName) Then
            DemoDB = Server.Databases(DemoDatabaseName)
            Console.WriteLine("AspectDemo DB exists")
        Else
            DemoDB = New Database(Server, DemoDatabaseName)
            DemoDB.Create()
            Console.WriteLine("AspectDemo DB created")
        End If
        DemoDB.ExecuteNonQuery(Resources.GetSQL(SQL.DemoDatabaseSchema))
        DemoDB.ExecuteNonQuery(Resources.GetSQL(SQL.DemoStoredProcedures))
        Console.WriteLine("AspectDemo DB refreshed")
        Console.WriteLine()
    End Sub

    Private Sub UpdateDemoSprocContracts()
        Console.WriteLine("updating contracts...")
        SqlManager.UpdateAllSqlDataCallContracts(ThisAssembly)
        Console.WriteLine("contracts updated")
        Console.WriteLine()
    End Sub

    Private Sub ExecuteProcs()

        'TODO: translate these into proper tests

        Console.WriteLine("adding products")
        Dim p1 As IProductInfo = Product.GetNew("Product 1")
        Dim p2 As IProductInfo = Product.GetNew("Product 2")
        Dim p3 As IProductInfo = Product.GetNew("Product 3")
        p3.DeleteThisProduct()
        Dim ps() As IProductInfo = Product.GetAll()
        Console.WriteLine()

        Console.WriteLine("adding customers")
        Dim c1 As ICustomerInfo = Customer.GetNew("New Customer 1")
        Dim c2 As ICustomerInfo = Customer.GetNew("New Customer 2")
        Dim c3 As ICustomerInfo = Customer.GetNew("New Customer 3")
        c3.DeleteThisCustomer()
        Dim cs() As ICustomerInfo = Customer.GetAll()
        Console.WriteLine()

        Console.WriteLine("adding sales orders")
        Dim s1 As ISalesOrderInfo = c1.AddSalesOrder(Now)
        Dim s2 As ISalesOrderInfo = c1.AddSalesOrder(Now)
        Dim s3 As ISalesOrderInfo = c2.AddSalesOrder(Now)
        s3.DeleteThisSalesOrder()
        Dim ss() As ISalesOrderInfo = SalesOrder.GetAll()
        Console.WriteLine()

        Console.WriteLine("adding sales order lines")
        Dim sl1 As ISalesOrderLineInfo = s1.AddSalesOrderLine(p1, 0.3D)
        Dim sl2 As ISalesOrderLineInfo = s1.AddSalesOrderLine(p2, 5.8D)
        Dim sl3 As ISalesOrderLineInfo = s2.AddSalesOrderLine(p2, 17.4D)
        sl3.DeleteThisSalesOrderLine()
        Dim sls() As ISalesOrderLineInfo = SalesOrderLine.GetAll()
        Console.WriteLine()

        Console.WriteLine("getting sales order line DTO-style objects")
        Dim slDTOs() As ISalesOrderLineFullInfo = SalesOrderLine.GetAllWithFullInfo()

    End Sub

End Module